/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package banksystem;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author conra_000
 */
public class WithdrawForm extends javax.swing.JFrame {
	long mAccountID;
	double mAccountBalance;
	Controller mController;
	
	/**
	 * Creates new form WithdrawForm
	 */
	public WithdrawForm(Controller controller, Account account ) {
		initComponents();
		
		mAccountID = account.getID();
		mAccountBalance = account.getBalance();
		mController = controller;
		jTextField2.setText(Double.toString(mAccountBalance));
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setMaximumSize(new java.awt.Dimension(384, 280));
        setMinimumSize(new java.awt.Dimension(384, 280));
        setPreferredSize(new java.awt.Dimension(384, 257));
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
        getContentPane().setLayout(null);

        jButton3.setText("Cancel");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(200, 210, 100, 30);

        jButton1.setText("Submit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(80, 210, 100, 30);

        jTextField3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jTextField3.setText(jTextField2.getText());
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });
        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField3KeyPressed(evt);
            }
        });
        getContentPane().add(jTextField3);
        jTextField3.setBounds(120, 150, 190, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("How much would you like to widthdraw?");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(30, 130, 290, 17);

        jTextField2.setEditable(false);
        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jTextField2.setText("2000");
        getContentPane().add(jTextField2);
        jTextField2.setBounds(120, 90, 190, 30);

        jLabel3.setBackground(new java.awt.Color(102, 102, 255));
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Current Funds:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(30, 60, 120, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
        jLabel2.setText("Withdraw Form");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(110, 20, 190, 25);

        jLabel5.setFocusable(false);
        jLabel5.setOpaque(true);
        getContentPane().add(jLabel5);
        jLabel5.setBounds(0, 0, 0, 0);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed

	}//GEN-LAST:event_jTextField3ActionPerformed

	private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyPressed

	}//GEN-LAST:event_jTextField3KeyPressed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		boolean outSuccess = false;
		
		if(jTextField3.getText().isEmpty())
			{
			// ALERT : NO VALUE ENTERED
			//JOptionPane.showMessageDialog(null,"You have not entered a value.");
			}
		else
			{
			try 
				{
				double tempValue1 = Double.parseDouble(jTextField3.getText().toString());
				this.mController.WithdrawFromAccount(tempValue1);
				double tempValue2 = Double.parseDouble(jTextField2.getText().toString());
					if(tempValue1 > 0)
						{
						if(tempValue1 <= Double.parseDouble(jTextField2.getText().toString()))
							{
							//SUCCESS
							outSuccess = true;
							//ALERT: Success
							//JOptionPane.showMessageDialog(null,"Success");
							tempValue1 *= 100.0;
							tempValue1 = (int)tempValue1;
							tempValue1 /= 100.0;
							tempValue2 -= tempValue1;
							jTextField2.setText("" + tempValue2);
							jTextField3.setText("" + tempValue1);
							}
						else
							{
							//ALERT : insufficient funds
							//JOptionPane.showMessageDialog(null,"Insufficient Funds");
							jTextField3.setText(jTextField2.getText());
							}
						}
					else
						{
						//ALERT : Value must be greater than 0
						//JOptionPane.showMessageDialog(null,"Must be greater than 0");
						jTextField3.setText("0");
						}
				} 
		catch (NumberFormatException e) 
			{
			//Alert : NOT A NUMBER
			//JOptionPane.showMessageDialog(null,"Not a number");
			jTextField3.setText("");
			}
			}
	}//GEN-LAST:event_jButton1ActionPerformed

	private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
		this.mController.SaveAndRestart(this);
	}//GEN-LAST:event_jButton3MouseClicked

	private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
		// TODO add your handling code here:
	}//GEN-LAST:event_formWindowActivated


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
